/*
 * TepcoMeter:TepcoMetterActivity.java
 * Copyright © 2011 ho9ho9 All Rights Reserved.
 */
package com.ho9ho9.tepcometer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.ho9ho9.tepcometer.dto.WattDto;
import com.ho9ho9.tepcometer.logic.TepcoMetterLogic;
import com.ho9ho9.tepcometer.view.GraphView;

/**
 * TepcoMetterActivityクラス.
 * @author ho9ho9
 */
public class TepcoMetterActivity extends Activity {

	private static final String TAG_NAME = "TepcoMetterActivity";
	DateFormat CORRESPOND_DAY_FORMAT = new SimpleDateFormat("yyyy/MM/dd");
	DateFormat UPDATE_DAY_FORMAT = new SimpleDateFormat("yyyy/MM/dd");
	DateFormat UPDATE_TIME_FORMAT = new SimpleDateFormat("HH:mm");
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity);
		
		// 解像度情報取得
		Display display = getWindowManager().getDefaultDisplay();
		int height = display.getHeight();
		int width = display.getWidth();
		Log.d(TAG_NAME, "h:" + height);
		Log.d(TAG_NAME, "w:" + width);
		
		// 電力情報取得
		TepcoMetterLogic logic = new TepcoMetterLogic();
		WattDto wattDto = logic.getToDayWattInfo(this);
		
		// レイアウト情報設定
		// 日付TextView
		TextView correspondDay = (TextView) findViewById(R.id.activityCorrespondDay);
		correspondDay.setText(CORRESPOND_DAY_FORMAT.format(wattDto.getWattDay()));
		TextView lastUpdateDay = (TextView) findViewById(R.id.activityUpdateDay);
		lastUpdateDay.setText(UPDATE_DAY_FORMAT.format(wattDto.getUpdated()));
		TextView lastUpdateTime = (TextView) findViewById(R.id.activityUpdateTime);
		lastUpdateTime.setText(UPDATE_TIME_FORMAT.format(wattDto.getUpdated()));
		// グラフView
		Configuration config = getResources().getConfiguration();
		if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
			// 横向き
			GraphView graphView = new GraphView(this);
			graphView.setWattDto(wattDto);
			graphView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
			LinearLayout main = (LinearLayout) findViewById(R.id.activityMainLayout);
			main.addView(graphView);
		} else {
			// 縦向き
			GraphView graphViewMorning = new GraphView(this);
			graphViewMorning.setWattDto(logic.getMorningWattDto(wattDto));
			graphViewMorning.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
			GraphView graphViewAfternoon = new GraphView(this);
			graphViewAfternoon.setWattDto(logic.getAfternoonWattDto(wattDto));
			graphViewAfternoon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
			LinearLayout main = (LinearLayout) findViewById(R.id.activityMainLayout);
			main.addView(graphViewMorning);
			main.addView(graphViewAfternoon);
		}
		// ウェジェットの更新
		RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget);
		logic.reflectWidgetViews(getResources(), views, wattDto);
	}

}
